import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grab_the_order/app/data/services/rule_service.dart';
import 'package:grab_the_order/app/modules/rule_model.dart';

class RuleController extends GetxController {
  // 获取已注入的 RuleService 实例
  final RuleService ruleService = Get.find<RuleService>();

  // 切换规则启用状态
  void toggleRuleStatus(RuleModel rule, bool isEnabled) {
    rule.isEnabled = isEnabled;
    ruleService.updateRule(rule);
  }

  // 删除规则
  void deleteRule(String id) {
    Get.defaultDialog(
      title: "确认删除",
      middleText: "您确定要删除这条规则吗？",
      textConfirm: "删除",
      textCancel: "取消",
      confirmTextColor: Colors.white,
      onConfirm: () {
        ruleService.deleteRule(id);
        Get.back();
      },
    );
  }

  // 显示添加/编辑规则的对话框
  void showAddRuleDialog() => _showRuleDialog();
  void showEditRuleDialog(RuleModel rule) =>
      _showRuleDialog(existingRule: rule);

  void _showRuleDialog({RuleModel? existingRule}) {
    final isEditing = existingRule != null;
    final minAmountController =
        TextEditingController(text: existingRule?.minAmount?.toString() ?? '');
    final maxDistanceController = TextEditingController(
        text: existingRule?.maxDistance?.toString() ?? '');

    Get.dialog(
      AlertDialog(
        title: Text(isEditing ? '编辑规则' : '添加新规则'),
        content: SingleChildScrollView(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 【这里只实现滴滴的规则，作为一个示例】
              const Text("目标应用: 滴滴出行",
                  style: TextStyle(fontWeight: FontWeight.bold)),
              SizedBox(height: 16),
              TextField(
                controller: minAmountController,
                keyboardType:
                    const TextInputType.numberWithOptions(decimal: true),
                decoration: const InputDecoration(
                  labelText: '最小金额 (元)',
                  hintText: '例如: 30',
                  border: OutlineInputBorder(),
                ),
              ),
              const SizedBox(height: 16),
              TextField(
                controller: maxDistanceController,
                keyboardType:
                    const TextInputType.numberWithOptions(decimal: true),
                decoration: const InputDecoration(
                  labelText: '最大路程 (公里)',
                  hintText: '例如: 10',
                  border: OutlineInputBorder(),
                ),
              ),
            ],
          ),
        ),
        actions: [
          TextButton(onPressed: () => Get.back(), child: const Text('取消')),
          ElevatedButton(
            onPressed: () {
              final rule = RuleModel(
                id: existingRule?.id ?? '', // 编辑时使用旧ID
                targetApp: TargetApp.didi,
                minAmount: double.tryParse(minAmountController.text),
                maxDistance: double.tryParse(maxDistanceController.text),
              );
              if (isEditing) {
                ruleService.updateRule(rule);
              } else {
                ruleService.addRule(rule);
              }
              Get.back();
            },
            child: const Text('保存'),
          ),
        ],
      ),
    );
  }
}
